package com.zhenmaitang.clinic_sys.common;

/**
 * 认证业务专用状态码枚举
 * 专门处理登录、注册等认证相关的业务状态码
 */
public enum AuthResultCode implements CodeEnum{
    
    // 登录成功状态码
    LOGIN_SUCCESS(2001, "登录成功"),
    REGISTER_SUCCESS(2002, "注册成功"),
    
    // 登录失败状态码
    INVALID_CREDENTIALS(4031, "用户名或密码错误"),
    USER_NOT_FOUND(4032, "用户不存在"),
    ACCOUNT_DISABLED(4033, "账号已被禁用"),
    ACCOUNT_LOCKED(4034, "账号已被锁定"),
    ACCOUNT_EXPIRED(4035, "账号已过期"),
    PASSWORD_EXPIRED(4036, "密码已过期"),
    
    // 注册失败状态码
    USERNAME_EXISTS(4091, "用户名已存在"),
    EMAIL_EXISTS(4092, "邮箱已存在"),
    PHONE_EXISTS(4093, "手机号已存在"),
    
    // 通用认证错误
    TOKEN_EXPIRED(4011, "登录已过期"),
    TOKEN_INVALID(4012, "无效的登录凭证"),
    PERMISSION_DENIED(4051, "权限不足"),

    SERVICE_UNAVAILABLE(5001,"未知异常");
    
    private final int code;
    private final String message;
    
    AuthResultCode(int code, String message) {
        this.code = code;
        this.message = message;
    }
    
    @Override
    public int getCode() {
        return code;
    }
    
    @Override
    public String getMessage() {
        return message;
    }
    
    public static AuthResultCode valueOf(int code) {
        for (AuthResultCode resultCode : values()) {
            if (resultCode.getCode() == code) {
                return resultCode;
            }
        }
        return INVALID_CREDENTIALS;
    }
    
    /**
     * 根据异常类型获取对应的业务状态码
     */
    public static AuthResultCode fromException(String exceptionMessage) {
        if (exceptionMessage == null) {
            return INVALID_CREDENTIALS;
        }
        
        String message = exceptionMessage.toLowerCase();
        
        if (message.contains("用户名或密码错误") || message.contains("密码错误")) {
            return INVALID_CREDENTIALS;
        } else if (message.contains("账号已被禁用") || message.contains("禁用")) {
            return ACCOUNT_DISABLED;
        } else if (message.contains("账号已被锁定") || message.contains("锁定")) {
            return ACCOUNT_LOCKED;
        } else if (message.contains("账号已过期") || message.contains("过期")) {
            return ACCOUNT_EXPIRED;
        } else if (message.contains("密码已过期")) {
            return PASSWORD_EXPIRED;
        } else if (message.contains("用户名已存在")) {
            return USERNAME_EXISTS;
        } else if (message.contains("邮箱已存在")) {
            return EMAIL_EXISTS;
        } else if (message.contains("手机号已存在")) {
            return PHONE_EXISTS;
        } else if (message.contains("登录已过期") || message.contains("token过期")) {
            return TOKEN_EXPIRED;
        } else if (message.contains("无效的登录凭证") || message.contains("token无效")) {
            return TOKEN_INVALID;
        } else if (message.contains("权限不足")) {
            return PERMISSION_DENIED;
        }else if (message.contains("用户不存在")) {
            return USER_NOT_FOUND;
        }else if (message.contains("未知异常")) {
            return SERVICE_UNAVAILABLE;
        }
        
        return INVALID_CREDENTIALS;
    }
}